# Create your models here.
from django.contrib.auth.models import User
from django.db import models
from utils.modelsMixin import ModelMixin, DateTimeModelMixin


# Create your models here.

# 商品分类表
# 1  python基础  null
# 2      变量    1
# 3      判断    1
# 4  数据库      null
# 5      mysql  4
class Classification(ModelMixin):
    name = models.CharField(max_length=100, unique=True, verbose_name="分类名")
    # 上一级 父级
    parent = models.ForeignKey("self", on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'classification'

    def __str__(self):
        return f'{self.id},{self.name}'


# 商品模型
class Commodity(ModelMixin):
    STATUS_CHOICES = ((0, "未发布"), (1, "发布"))
    name = models.CharField(max_length=100, verbose_name="商品名", unique=True)
    caption = models.CharField(max_length=100, verbose_name="副标题")
    brand = models.CharField(max_length=100, verbose_name="品牌", null=True, blank=True)
    price = models.DecimalField(max_digits=12, decimal_places=2, verbose_name="价格")
    stock = models.IntegerField(verbose_name="库存")
    pack = models.TextField(verbose_name="包装信息", null=True, blank=True)
    sales = models.IntegerField(verbose_name="销量", default=0)
    comments = models.IntegerField(default=0, verbose_name="评价数")
    status = models.IntegerField(default=0, verbose_name="状态", choices=STATUS_CHOICES)
    detail = models.TextField(verbose_name="详情", blank=True, null=True)
    serviceafter_sale = models.TextField(verbose_name="售后服务")
    # classification_set ==>classification1
    classification1 = models.ForeignKey(Classification, on_delete=models.SET_NULL, null=True, blank=True,
                                        related_name="classification1")
    # classification_set
    classification2 = models.ForeignKey(Classification, on_delete=models.SET_NULL, null=True, blank=True,
                                        related_name="classification2")

    class Meta:
        db_table = 'commodity'
        ordering = ["-sales", "-comments", "-created_time"]


# 商品图
class CommodityImg(ModelMixin):
    src = models.CharField(max_length=100, null=True, blank=True)
    priority = models.IntegerField(default=0, verbose_name="优先级")
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        db_table = 'commodityimg'
        ordering = ["-priority"]
